package com.vader.flea.service.impl;

import com.vader.flea.dao.ProductDao;
import com.vader.flea.dao.ProductImgDao;
import com.vader.flea.dto.ImgHolder;
import com.vader.flea.dto.ProductExecution;
import com.vader.flea.entity.Product;
import com.vader.flea.entity.ProductImg;
import com.vader.flea.enums.ProductStateEnum;
import com.vader.flea.exceptions.ProductCategoryOperationException;
import com.vader.flea.exceptions.ProductOperationException;
import com.vader.flea.service.ProductService;
import com.vader.flea.util.ImgUtil;
import com.vader.flea.util.PageCalculator;
import com.vader.flea.util.PathUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductDao productDao;

    @Autowired
    private ProductImgDao productImgDao;

    @Override
    @Transactional
    public ProductExecution addProduct(Product product, ImgHolder thumbnail,
                                       List<ImgHolder> productImgList) throws ProductOperationException {

        if (product != null && product.getShop() != null && product.getShop().getShopId() != null) {
            product.setCreateTime(new Date());
            product.setUpdateTime(new Date());
            product.setEnableStatus(1);
            if (thumbnail != null) {
                addThumbnail(product, thumbnail);
            }
            try {
                int rows = productDao.insertProduct(product);
                if (rows <= 0) {
                    throw new ProductOperationException("商品添加失败");
                }
            } catch (Exception e) {
                throw new ProductOperationException("商品添加失败" + e.getMessage());
            }
            if (productImgList != null && productImgList.size() > 0) {
                addProductImgList(product, productImgList);
            }
            return new ProductExecution(ProductStateEnum.SUCCESS, product);
        } else {
            return new ProductExecution(ProductStateEnum.EMPTY);
        }
    }

    @Override
    public Product getProductById(Long productId) {
        Product product = productDao.queryProductById(productId);
        return product;
    }

    @Override
    public ProductExecution modifyProduct(Product product, ImgHolder thumbnail,
                                          List<ImgHolder> productImgHolderList) throws ProductOperationException {
        if (product != null && product.getShop() != null && product.getShop().getShopId() != null) {
            product.setUpdateTime(new Date());
            if (thumbnail != null) {
                Product tempProduct = productDao.queryProductById(product.getProductId());
                if (tempProduct.getImgAddr() != null) {
                    ImgUtil.deleteFileOrPath(tempProduct.getImgAddr());
                }
                addThumbnail(product, thumbnail);
            }
            if (productImgHolderList != null && productImgHolderList.size() > 0) {
                deleteProductImgList(product.getProductId());
                addProductImgList(product, productImgHolderList);
            }

            try {
                int rows = productDao.updateProduct(product);
                if (rows <= 0) {
                    throw new ProductOperationException("更新商品信息失败");
                }
                return new ProductExecution(ProductStateEnum.SUCCESS, product);
            } catch (Exception e) {
                throw new ProductOperationException("商品信息更新失败" + e.toString());
            }
        } else {
            return new ProductExecution(ProductStateEnum.EMPTY);
        }
    }

    @Override
    public ProductExecution getProductList(Product productCondition, int pageIndex, int pageSize) {
        int rowIndex = PageCalculator.calculateRowIndex(pageIndex,pageSize);
        List<Product> productList = productDao.queryProductList(productCondition, rowIndex, pageSize);
        int count = productDao.queryProductCount(productCondition);
        ProductExecution productExecution = new ProductExecution();
        productExecution.setProductList(productList);
        productExecution.setCount(count);
        return productExecution;
    }

    @Override
    public int queryProductCount(Product productCondition) {
        return 0;
    }

    @Override
    public void deleteProductImgList(Long productId) {
        List<ProductImg> productImgList = productImgDao.queryProductImgList(productId);

        for (ProductImg pi : productImgList) {
            ImgUtil.deleteFileOrPath(pi.getImgAddr());
        }
        productImgDao.deleteProductImgByProductId(productId);
    }

    private void addProductImgList(Product product, List<ImgHolder> productImgList) {
        String dest = PathUtil.getShopImagePath(product.getShop().getShopId());
        List<ProductImg> productImgs = new ArrayList<ProductImg>();
        for (ImgHolder pi : productImgList) {
            String imgAddr = ImgUtil.generateNormalImg(pi, dest);
            ProductImg productImg = new ProductImg();
            productImg.setImgAddr(imgAddr);
            productImg.setProductId(product.getProductId());
            productImg.setCreateTime(new Date());
            productImgs.add(productImg);
        }

        if (productImgs.size() > 0) {
            try {
                int rows = productImgDao.batchInsertProductImg(productImgs);
                if (rows <= 0) {
                    throw new ProductOperationException("商品详情图片添加失败");
                }
            } catch (Exception e) {
                throw new ProductOperationException("商品详情图片添加失败" + e.toString());
            }
        }
    }

    private void addThumbnail(Product product, ImgHolder thumbnail) {
        String dest = PathUtil.getShopImagePath(product.getShop().getShopId());
        String thumbnailAddr = ImgUtil.generateThumbnail(thumbnail, dest);
        product.setImgAddr(thumbnailAddr);
    }
}

